Week 3 Project

Colin Murphy

11/4/2021

Set Up

suppressPackageStartupMessages(library(plotly))
suppressPackageStartupMessages(library(data.table))
suppressPackageStartupMessages(library(maps))
dt <- data.table(mtcars, keep.rownames = T)

Plotting with Plotly

Use the plotly package to make some cool graphics!

plot_ly(data = dt, x = ~disp, y =  ~mpg, type = 'scatter', name = ~rn, color = ~qsec, showlegend = F)

More Dimensions!

plot_ly(data = dt, x = ~disp, y = ~mpg, z = ~qsec, type = 'scatter3d', split = ~cyl, legendgrouptitle = list(text = 'Number of Cylinders'), hovertext = ~rn, hoverinfo = 'x+y+z+text')

Maps Too!

avg.repub <- apply(votes.repub, MARGIN = 1, function(x) mean(x, na.rm = T)) %>%
  as.data.table(keep.rownames = T)
avg.repub[, states := state.abb]
  plot_ly(avg.repub,  z = ~., type = 'choropleth', color = ~., locations = ~states, locationmode = 'USA-states', colors = 'Reds') %>%  
    layout(title = 'Average Republican Vote Percentage by State (1856 - 1976)', geo = list(scope = 'usa', projection = list(type = 'albers usa')))